﻿using huqiang.Communication;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace huqiang.DataSet
{
    public class RecordFile : IDisposable
    {
        protected FileStream stream;
        protected int Day;
        public string Folder { get; private set; }
        public string Name { get; private set; }
        private RecordFile()
        {
        }
        private RecordFile(string folder, string name)
        {
            Folder = folder;
            Name = name;
            string dic = "Record";
            if (!Directory.Exists(dic))
                Directory.CreateDirectory(dic);
            var fs = folder.Split('/');
            for (int i = 0; i < fs.Length; i++)
            {
                dic = dic + "/" + fs[i];
                if (!Directory.Exists(dic))
                    Directory.CreateDirectory(dic);
            }
            string path = dic + "/" + name;
            try
            {
                stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
            }
            catch
            {
                OutLog.WriteLine("记录文件打开失败");
            }
        }
        ~RecordFile()
        {
            Dispose();
        }
        public void Write(string msg)
        {
            if (stream == null)
                return;
            var buf = Encoding.UTF8.GetBytes(msg);
            stream.Write(buf, 0, buf.Length);
            stream.Flush();
        }
        public void Write(params string[] msg)
        {
            if (stream == null)
                return;
            for (int i = 0; i < msg.Length; i++)
            {
                var buf = Encoding.UTF8.GetBytes(msg[i]);
                stream.Write(buf, 0, buf.Length);
            }
            stream.Flush();
        }
        public void Dispose()
        {
            Close(Folder,Name);
        }
        private void Clear()
        {
            if (stream != null)
            {
                stream.Dispose();
                stream = null;
            }
        }
        static List<RecordFile> records;
        public static RecordFile Open(string folder, string name)
        {
            if(records==null)
            {
                records = new List<RecordFile>();
                var r = new RecordFile(folder,name);
                records.Add(r);
                return r;
            }
            else
            {
                for(int i=0;i<records.Count;i++)
                {
                    if (records[i].Folder == folder)
                        if (records[i].Name == name)
                            return records[i];
                }
            }
            var rf = new RecordFile(folder, name);
            records.Add(rf);
            return rf;
        }
        public static void Close(string folder, string name)
        {
            if(records!=null)
            {
                for (int i = 0; i < records.Count; i++)
                {
                    var r = records[i];
                    if (r.Folder == folder)
                        if (r.Name == name)
                        {
                            records.RemoveAt(i);
                            r.Clear();
                            return;
                        }
                }
            }
        }
    }
}
